﻿using System;
using System.Linq;
using System.Linq.Expressions;
using System.Linq.Dynamic;using System.Text;
using System.Data;
using System.Data.Linq;
using System.Transactions;
using LinqToSqlExtensions;using System.ServiceModel;
using System.Collections;
using System.Collections.Generic;
using CardsServer.DAL;
using CardsServer.Tools;
namespace CardsServer.WCF
{
    public partial interface IService
    {

        #region 收款单
        
        [OperationContract]
        List<SK_D> getNotSK_By_ClientID(Guid clientid,int ACID);

        [OperationContract]
        string sk_all_add(SK sk, List<SK_D> skd, List<SK_I> ski, int ACID);

        #endregion

        #region 付款单

        [OperationContract]
        List<FK_D> getNotFK_By_ClientID(Guid clientid, int ACID);

        [OperationContract]
        string fk_all_add(FK fk, List<FK_D> fkd, List<FK_I> fki, int ACID);

        #endregion

    }
    public partial class Service : IService
    {

        #region 收款单实现方法

        /// <summary>
        ///查询收款单据
        /// </summary>
        /// <param name="clientid">客户查询</param>
        /// <param name="ACID"></param>
        /// <returns></returns>
        public List<SK_D> getNotSK_By_ClientID(Guid clientid,int ACID)
        {
            using (CardsServer.DAL.DataClassesDataContext ctx = new CardsServer.DAL.DataClassesDataContext(getAccConnStr(ACID)))
            {
                List<SK_D> skd = new List<SK_D>();
                foreach (var item in CardOpen_M_Get(p => p.ClientID == clientid && p.isfinish == false, "", ACID))
                {
                    SK_D d = new SK_D();
                    d.billpid = item.pid;
                    d.billtype = 4;
                    d.blance = item.balance;
                    d.jsje = 0;
                    d.Pdate = item.Pdate;
                    d.discount = item.discount;
                    d.pid = "";
                    d.ysje = item.total;
                    d.selected = false;
                    skd.Add(d);
                }
                //空卡销售单据
                foreach (var item in KCSale_M_Get(p => p.ClientID == clientid && p.isfinish == false, "", ACID))
                {
                    SK_D d = new SK_D();
                    d.billpid = item.pid;
                    d.billtype = 5;
                    d.blance = item.balance;
                    d.jsje = 0;
                    d.Pdate = item.Pdate;
                    d.discount = item.discount;
                    d.pid = "";
                    d.ysje = item.total;
                    d.selected = false;
                    skd.Add(d);
                }
                foreach (var item in CZ_M_Get(p => p.ClientID == clientid && p.isfinish == false, "", ACID))
                {
                    SK_D d = new SK_D();
                    d.billpid = item.pid;
                    d.billtype = 6;
                    d.blance = item.balance;
                    d.jsje = 0;
                    d.Pdate = item.Pdate;
                    d.discount = item.discount;
                    d.pid = "";
                    d.ysje = item.total;
                    skd.Add(d);
                }
                foreach (var item in maintain_Get(p => p.clientID == clientid && p.S_isfinished == false, "", ACID))
                {
                    SK_D d = new SK_D();
                    d.billpid = item.pid;
                    d.billtype = 7;
                    d.blance = item.S_balance;
                    d.discount = item.S_discount;
                    d.jsje = 0;
                    d.Pdate = item.Pdate;
                    d.pid = "";
                    d.ysje = item.S_total;
                    skd.Add(d);
                }
                //产品销售单据
                foreach (var item in ProductSale_M_Get(p => p.clientID == clientid && p.isfinish == false && p.checkstatus == "已通过", "", ACID))
                {
                    SK_D d = new SK_D();
                    d.billpid = item.djn;
                    d.billtype = 13;
                    d.blance = item.banlance.Value - item.ysmoney.Value;
                    d.jsje = 0;
                    d.Pdate = item.odate;
                    d.discount = item.discount;
                    d.pid = "";
                    d.ysje = item.banlance.Value;
                    d.selected = false;
                    skd.Add(d);
                }
                return skd;
            }
        }

        /// <summary>
        /// 保存收款单
        /// </summary>
        /// <param name="sk"></param>
        /// <param name="skd"></param>
        /// <param name="ski"></param>
        /// <param name="ACID"></param>
        /// <returns></returns>
        public string sk_all_add(SK sk, List<SK_D> skd, List<SK_I> ski, int ACID)
        { 
            string r = "";
            using (TransactionScope tc = new TransactionScope())
            {
                try
                {
                    using (CardsServer.DAL.DataClassesDataContext ctx = new CardsServer.DAL.DataClassesDataContext(getAccConnStr(ACID)))
                    {
                        r = SK_Add(sk, ACID, ctx);
                        if (r != "") return r;
                        foreach (var item in skd)
                        {
                            item.pid = sk.pid;
                            r = SK_D_Add(item, ACID, ctx);
                            if (r != "") return r;
                            switch (item.billtype)
                            {
                                case 4:
                                    CardOpen_M m4 = ctx.CardOpen_M.Single(p => p.pid == item.billpid);
                                    if (m4.balance == item.jsje)
                                    {
                                        m4.balance = 0;
                                        m4.isfinish = true;
                                    }
                                    else
                                    {
                                        m4.balance -= item.jsje;
                                    }
                                    break;
                                case 5:
                                    KCSale_M m5 = ctx.KCSale_M.Single(p => p.pid == item.billpid);
                                    if (m5.balance == item.jsje)
                                    {
                                        m5.balance = 0;
                                        m5.isfinish = true;
                                    }
                                    else
                                    {
                                        m5.balance -= item.jsje;
                                    }
                                    break;
                                case 6:
                                    CZ_M m6 = ctx.CZ_M.Single(p => p.pid == item.billpid);
                                    if (m6.balance == item.jsje)
                                    {
                                        m6.balance = 0;
                                        m6.isfinish = true;
                                    }
                                    else
                                    {
                                        m6.balance -= item.jsje;
                                    }
                                    break;
                                case 7:
                                    maintain m7 = ctx.maintain.Single(p => p.pid == item.billpid);
                                    if (m7.S_balance == item.jsje)
                                    {
                                        m7.S_balance = 0;
                                        m7.S_isfinished = true;
                                    }
                                    else
                                    {
                                        m7.S_balance -= item.jsje;
                                    }
                                    break;
                                case 13: //销售单
                                    ProductSale_M m13 = ctx.ProductSale_M.Single(p => p.djn == item.billpid);
                                    if (m13.banlance == item.jsje)
                                    {
                                        m13.banlance = 0;
                                        m13.isfinish = true;
                                    }
                                    else
                                    {
                                        m13.banlance -= item.jsje;
                                    }
                                    break;
                                default:
                                    break;
                            }
                        }
                        foreach (SK_I i in ski)
                        {
                            i.pid = sk.pid;
                            r = SK_I_Add(i, ACID, ctx);
                            if (r != "") return r;
                        }
                        RecordBalance(-sk.ssje, sk.ClientID, ctx, ACID);
                        ctx.SubmitChanges();
                        tc.Complete();
                    }
                }
                catch (Exception ex)
                {
                    r = ex.Message;
                }
            }
            return r;
        }

        #endregion

        #region 付款单实现方法

        /// <summary>
        /// 查询付款单据
        /// </summary>
        /// <param name="clientid"></param>
        /// <param name="ACID"></param>
        /// <returns></returns>
        public List<FK_D> getNotFK_By_ClientID(Guid clientid, int ACID)
        {
            using (CardsServer.DAL.DataClassesDataContext ctx = new CardsServer.DAL.DataClassesDataContext(getAccConnStr(ACID)))
            {
                List<FK_D> fkd = new List<FK_D>();

                //产品销售退货单据,过滤条件，客户和未完结的单据
                foreach (var item in ProductSaleBack_M_Get(p => p.clientID == clientid && p.isfinish == false, "", ACID))
                {
                    FK_D d = new FK_D();
                    d.billpid = item.djn;
                    d.billtype = 14;
                    d.blance = item.banlance.Value - item.yfmoney.Value;
                    d.jsje = 0;
                    d.Pdate = item.odate;
                    d.discount = item.discount;
                    d.pid = "";
                    d.yfje = item.banlance.Value;
                    d.selected = false;
                    fkd.Add(d);
                }
                return fkd;
            }
        }

        /// <summary>
        /// 付款单保存方法
        /// </summary>
        /// <param name="fk"></param>
        /// <param name="fkd"></param>
        /// <param name="fki"></param>
        /// <param name="ACID"></param>
        /// <returns></returns>
        public string fk_all_add(FK fk, List<FK_D> fkd, List<FK_I> fki, int ACID)
        {
            string r = "";
            using (TransactionScope tc = new TransactionScope())
            {
                try
                {
                    using (CardsServer.DAL.DataClassesDataContext ctx = new CardsServer.DAL.DataClassesDataContext(getAccConnStr(ACID)))
                    {
                        r = FK_Add(fk, ACID, ctx);
                        if (r != "")
                        {
                            return r;
                        }
                        foreach (var item in fkd)
                        {
                            item.pid = fk.pid;
                            r = FK_D_Add(item, ACID, ctx);
                            if (r != "")
                            {
                                return r;
                            }
                            switch (item.billtype)
                            {
                                case 14: //销售单
                                    ProductSaleBack_M m14 = ctx.ProductSaleBack_M.Single(p => p.djn == item.billpid);
                                    if (m14.banlance == item.jsje)
                                    {
                                        m14.banlance = 0;
                                        m14.isfinish = true;
                                    }
                                    else
                                    {
                                        m14.banlance -= item.jsje;
                                    }
                                    break;
                                default:
                                    break;
                            }
                        }
                        foreach (FK_I i in fki)
                        {
                            i.pid = fk.pid;
                            r= FK_I_Add(i, ACID, ctx);
                            if (r!="")
                            {
                                return r;
                            }
                        }
                        RecordBalance(-fk.sfje, fk.ClientID, ctx, ACID);
                        ctx.SubmitChanges();
                        tc.Complete();
                    }
                }
                catch (Exception ex)
                {
                    r = ex.Message;
                }
            }
            return r;
        }

        #endregion
    }


}

